<template>
  <div style="margin-top:20px">
    <!-- ----------------------------------opcua------------------------------------------->
    <el-table ref="multipleTable" height="520" border :data="rule_map" size='mini'>
      <template slot="empty">
        <el-empty :description="$t('暂无数据')"></el-empty>
      </template>
      <el-table-column prop="rule_id" align='center' :label="$t('规则ID')" width="80" />
      <el-table-column prop="proto" align='center' :label="$t('协议名称')" width="120" />
      <el-table-column prop="action" align='center' :label="$t('动作')" width="80">
        <template slot-scope="scope">
          {{ $t(actionList[scope.row.action]) }}
        </template>
      </el-table-column>
      <el-table-column prop="msg_type" align='center' :label="$t('消息类型')" width="80">
        <template slot-scope="scope">
          {{ scope.row.msg_type }}
        </template>
      </el-table-column>
      <el-table-column prop="service_id" align='center' :label="$t('服务码')" min-width="150">
        <template slot-scope="scope">
          <tooltip :copyText="formatterServer(scope.row, 'column', scope.row.service_id, serviceIdList)" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <!-- <el-table-column prop="node_id_mask" align='center' :formatter="(row, column, cellValue) => formatter(row, column, cellValue, nodeIdMaskList)" :label="$t('节点类型')" width="80" /> -->
      <el-table-column prop="node_id" align='center' :label="$t('节点标识')" width="80">
        <template slot-scope="scope">
          <tooltip :copyText="scope.row.node_id" class="wid190">
          </tooltip>
        </template>
      </el-table-column>
      <el-table-column prop="data_mask" align='center' :label="$t('数据值类型')" :formatter="(row, column, cellValue) => formatter(row, column, cellValue, dataMaskList)" width="100" />
      <el-table-column prop="value_range" align='center' :formatter="valueRangeFormatter" width="140" :label="$t('取值范围')" />
      <el-table-column prop="accept_range" align='center' :label="$t('匹配范围')" width="100">
        <template slot-scope="scope">
          {{ ['HEL', 'ACK', 'ERR'].includes(scope.row.msg_type)?'':acceptRangeList[scope.row.accept_range] }}
        </template>
      </el-table-column>
      <el-table-column align="center" :label="$t('操作')" width="160" fixed='right'>
        <template slot-scope="scope">
          <el-button type="success" plain @click="editRule(scope.row)" size='mini' :class="'edit' +(scope.$index+1)">{{$t('编辑')}}</el-button>
          <el-button type="danger" plain @click="deleteRule(scope.row)" size='mini' :class="'del' +(scope.$index+1)">{{$t('删除')}}</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>
<script>
import ruleArray from '../../ruleArray'
export default {
  name: 'OpcuaTable',
  props: {
    ruleMap: {
      type: Array,
      default () {
        return []
      }
    }
  },
  watch: {
    ruleMap: {
      handler (newValue, oldValue) {
        this.rule_map = []
        newValue.forEach(val => {
          if (val.proto === 'opcua') {
            this.rule_map.push(val)
          }
        })
      },
      deep: true // 深度监听
    }
  },
  data () {
    return {
      dialogVisible: false,
      ruleDialogVisible: false,
      rule_map: [],
      serviceIdList: ruleArray.serviceIdShowList,
      nodeIdMaskList: ruleArray.nodeIdMaskList,
      dataMaskList: ruleArray.dataMaskList,
      msgTypeShowList: ruleArray.msgTypeShowList,
      valueRangeList: {
        '0': 'false',
        '1': 'true'
      },
      actionList: { 'accept': '允许', 'alert': '告警' },
      acceptRangeList: ruleArray.acceptRangeList
    }
  },
  mounted () {
    this.rule_map = []
    this.ruleMap.forEach(val => {
      if (val.proto === 'opcua') {
        this.rule_map.push(val)
      }
    })
  },
  methods: {
    deleteRule (row) {
      this.$emit('delRuleMap', row)
    },
    editRule (row) {
      this.$emit('largeConfig', row)
    },
    formatterServer (row, column, cellValue, list) {
      if (this.msgTypeShowList.includes(row.msg_type)) {
        return list[cellValue]
      }
    },
    formatter (row, column, cellValue, list) {
      if (this.msgTypeShowList.includes(row.msg_type) && row.accept_range !== 'all' && ['999999', '673', '676', '672', '675'].includes(row.service_id)) {
        return list[cellValue]
      }
    },
    valueRangeFormatter (data) {
      if (this.msgTypeShowList.includes(data.msg_type) && data.accept_range !== 'all' && ['999999', '673', '676', '672', '675'].includes(data.service_id)) {
        if (data.data_mask === '1') {
          return this.valueRangeList[data.value_range.split('~')[0]]
        } else {
          return data.value_range
        }
      }
    }
  }
}
</script>
